<#function dashedToCamel(s)>
    <#return s
    ?replace('(^_+)|(_+$)', '', 'r')
    ?replace('\\_+(\\w)?', ' $1', 'r')
    ?replace('([A-Z])', ' $1', 'r')
    ?capitalize
    ?replace(' ' , '')
    ?uncap_first
    >
</#function>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${cfg.package_name}.repository.mapper.base.${entity}BaseMapper">

    <#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
    </#if>
    <resultMap id="BaseResultMap" type="${cfg.package_name}.model.bean.${entity}" >
        <id column="${cfg.primaryKey}" property="${cfg.primaryKey}"/>
        <#list cfg.columnList as column>
        <#if column.isPrimaryKey=="false" && (column.tableAliasName=="" || column.tableAliasName=="t1")>
        <result column="${column.columnName}" property="${column.propertyName}" />
        </#if>
        </#list>

        <#if cfg.hasM2O>
        <!--
            Many to One: 实时加载
            association：用于映射关联查询单个对象的信息
            property：要讲关联查询的用户信息映射到订单pojo列中的哪个属性里
            javaType:表示关联查询的对象类型（可以使用别名）
            < column.columnNameSql!="t2.id" && column.tableAliasName=="t2">
         -->

            <#list cfg.tableNameM2OList as tableNameMap>
            <association property="${tableNameMap.classInstance}" javaType="${cfg.package_name}.model.bean.${tableNameMap.className}">
                <id column="${tableNameMap.tableName}_id" property="${cfg.primaryKey}"/>
                <#list cfg.columnList as column>
                <#if column.columnNameSql!=tableNameMap.alias+".id" && column.tableAliasName==tableNameMap.alias>
                <result column="${column.columnName}" property="${dashedToCamel(column.columnNameSql?substring(3))}" />
                </#if>
                </#list>
            </association>
            </#list>
        </#if>

    </resultMap>

    <!-- One to Many ：延迟加载信息列表-->
    <!-- 使用One to Many集合时，select 中 resultMap="BaseResultMapO2M" -->
    <resultMap id="BaseResultMapO2M" type="${cfg.package_name}.model.bean.${entity}" extends="BaseResultMap">
        <!--
            One to Many ：延迟加载信息列表
            select:指定延迟加载需要执行的sql配置id， 调用外部方法: 对应xml的namespace + 对应xml中的代码片段的id,
            column:column作为select语句的参数传入,如果只传一个参数id可以简写: column="id"
                   传多值：column="{qid=id,sort=sort}" qid/sort是定义的变量名, id/sort是主表的字段id/sort
            <collection property="a2userList" column="id"
                        ofType="${cfg.package_name}.model.bean.A2user"
                        select="${cfg.package_name}.repository.mapper.A2userMapper.mySelectByA2deptId" />
        -->
        <#list cfg.o2mList as o2m>
        <#if o2m.hasO2M>
        <collection
            fetchType="lazy"
            property="${o2m.classNameFirstWordLowerO2M}List"
            column="${o2m.column}"
            ofType="${cfg.package_name}.model.bean.${o2m.classNameFirstWordUpperO2M}"
            select="mySelect${o2m.classNameFirstWordUpperO2M}ListBy_${o2m.slaveColumn}" />
        </#if>
        </#list>
    </resultMap>


    <sql id="Base_Column_List" >
        ${cfg.columnStr?replace(",",",\n        ")}
    </sql>


    <#list cfg.o2mList as o2m>
    <#if o2m.hasO2M>
    <!-- one to many -->
    <select id="mySelect${o2m.classNameFirstWordUpperO2M}ListBy_${o2m.slaveColumn}" resultType="${cfg.package_name}.model.bean.${o2m.classNameFirstWordUpperO2M}" >
        select
            *
        from ${o2m.tableNameFirstWordLowerO2M} t1 where t1.${o2m.slaveColumn} = ${r"#"}{${o2m.column},jdbcType=BIGINT} and t1.deleteflag=0
    </select>
    </#if>
    </#list>


    <#list cfg.columnList as column>
        <#if column.dataResource?exists && column.dataResource==2 && column.optionsSql?exists && column.optionsSql!='' >
    <!-- 页面上查询列的值，动态加载，sql语句读数据库配置而来 -->
    <select id="mySelect4${column.propertyName}" resultType="java.util.Map">
        ${column.optionsSql}
    </select>
        </#if>
    </#list>

    <!-- 默认开启了One to Many加载，不需要话，可关闭：resultMap="BaseResultMap" -->
    <select id="myPage" resultMap="BaseResultMapO2M">
        select
        <include refid="Base_Column_List" />
        from ${cfg.tableNameSql} ${r"$"}{ew.customSqlSegment}
    </select>

    <select id="mySelectSum" resultMap="BaseResultMap">
        select
        <#if cfg.columnSumStr?length!=0>
            ${cfg.columnSumStr}
        <#else>
            count(t1.deleteflag) as id
        </#if>
        from ${cfg.tableNameSql} ${r"$"}{ew.customSqlSegment}
    </select>

    <select id="mySelectListCount" resultType="java.lang.Integer">
        select count(t1.deleteflag)
        from ${cfg.tableNameSql} ${r"$"}{ew.customSqlSegment}
    </select>

    <select id="mySelectBy" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from ${cfg.tableNameSql} ${r"$"}{ew.customSqlSegment}
    </select>

    <select id="mySelectById" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from ${cfg.tableNameSql} where t1.${cfg.primaryKey} = ${r"#"}{id,jdbcType=BIGINT}
    </select>

    <!--
    单表操作，建议使用自带封装的方法直接处理
    <delete id="myDeleteById" parameterType="${package.Entity}.${entity}" >
        update ${cfg.tableName}
        set  deleteflag=1,modifyman=${r"#"}{modifyman,jdbcType=VARCHAR},modifydate=${r"#"}{modifydate,jdbcType=TIMESTAMP}
        where ${cfg.primaryKey} = ${r"#"}${cfg.primaryKey},jdbcType=INTEGER}
    </delete>

    <insert id="myInsert" parameterType="${package.Entity}.${entity}" >
        insert into ${cfg.tableName}
        (${cfg.canAddColumnStr},createman_id,createman,createdate,company_id
        )
        values (${cfg.canAddColumnValueStr},${r"#{"}createmanId,jdbcType=VARCHAR},${r"#{"}createman,jdbcType=VARCHAR}
        ,${r"#{"}createdate,jdbcType=TIMESTAMP}, ${r"#{"}companyId,jdbcType=TIMESTAMP}
        )
    </insert>

    <update id="myUpdateById" parameterType="${package.Entity}.${entity}" >
        update ${cfg.tableName}
        set ${cfg.CanEditColumnStr},modifyman_id=${r"#{"}modifymanId,jdbcType=VARCHAR},modifyman=${r"#{"}modifyman,jdbcType=VARCHAR},modifydate=${r"#{"}modifydate,jdbcType=TIMESTAMP}
        where ${cfg.primaryKey} = ${r"#{"}${cfg.primaryKey},jdbcType=BIGINT}
    </update>
    -->


</mapper>
